package com.lib.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

/**
 * @author qbw
 * @createtime 2016/04/29 11:48
 */


public class NetUtil {
    /**
     * Unknown network class. {@hide}
     */
    public static final int NETWORK_CLASS_UNKNOWN = 0;
    /**
     * Class of broadly defined "2G" networks. {@hide}
     */
    public static final int NETWORK_CLASS_2_G = 1;
    /**
     * Class of broadly defined "3G" networks. {@hide}
     */
    public static final int NETWORK_CLASS_3_G = 2;
    /**
     * Class of broadly defined "4G" networks. {@hide}
     */
    public static final int NETWORK_CLASS_4_G = 3;

    /**
     * 中国移动
     **/
    public static final int CHINA_MOBILE_CMCC = 1;
    /**
     * 中国联通
     ***/
    public static final int CHINA_UNICOM = 2;
    /**
     * 中国电信
     **/
    public static final int CHINA_TELECOM = 3;
    /**
     * 运营商未知
     */
    public static final int UNKONW = 4;

    /**
     * @return 网络是否可用
     */
    public static boolean isNetAvailable(Context context) {
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(
                    Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            return null != networkInfo && networkInfo.isAvailable();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean isNetConnected(Context context) {
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(
                    Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            return null != networkInfo && networkInfo.isConnected();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static String getNetworkType(Context context) {
        if (isWifi(context)) {
            return "WiFi";
        } else {
            switch (getNetworkClass(context)) {
                case NETWORK_CLASS_4_G:
                    return "4G";
                case NETWORK_CLASS_3_G:
                    return "3G";
                case NETWORK_CLASS_2_G:
                    return "2G";
            }
        }
        return "Unknown";
    }

    public static boolean isWifi(Context context) {
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(
                    Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
            if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean is2G(Context context) {
        return NETWORK_CLASS_2_G == getNetworkClass(context);
    }

    public static boolean is3G(Context context) {
        return NETWORK_CLASS_3_G == getNetworkClass(context);
    }

    public static boolean is4G(Context context) {
        return NETWORK_CLASS_4_G == getNetworkClass(context);
    }

    private static int getNetworkClass(Context context) {
        try {

            int networkType = -1;
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(
                    Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
            if (activeNetInfo != null) {
                networkType = activeNetInfo.getSubtype();
            }
            switch (networkType) {
                case TelephonyManager.NETWORK_TYPE_GPRS:
                case TelephonyManager.NETWORK_TYPE_EDGE:
                case TelephonyManager.NETWORK_TYPE_CDMA:
                case TelephonyManager.NETWORK_TYPE_1xRTT:
                case TelephonyManager.NETWORK_TYPE_IDEN:
                    return NETWORK_CLASS_2_G;
                case TelephonyManager.NETWORK_TYPE_UMTS:
                case TelephonyManager.NETWORK_TYPE_EVDO_0:
                case TelephonyManager.NETWORK_TYPE_EVDO_A:
                case TelephonyManager.NETWORK_TYPE_HSDPA:
                case TelephonyManager.NETWORK_TYPE_HSUPA:
                case TelephonyManager.NETWORK_TYPE_HSPA:
                case TelephonyManager.NETWORK_TYPE_EVDO_B:
                case TelephonyManager.NETWORK_TYPE_EHRPD:
                case TelephonyManager.NETWORK_TYPE_HSPAP:
                    return NETWORK_CLASS_3_G;
                case TelephonyManager.NETWORK_TYPE_LTE:
                    return NETWORK_CLASS_4_G;
                default:
                    return NETWORK_CLASS_UNKNOWN;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return NETWORK_CLASS_UNKNOWN;
    }

    /**
     * 获取mac
     */
    public static String getMacFromWifi(Context context) {
        WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (wifi == null) {
            return "00:00:00:00:00:00";
        }
        WifiInfo info = wifi.getConnectionInfo();
        if (info == null) {
            return "00:00:00:00:00:00";
        }
        String wifiMac = info.getMacAddress();
        if (TextUtils.isEmpty(wifiMac)) {
            return "00:00:00:00:00:00";
        } else {
            return wifiMac;
        }
    }

    /***
     * 获取运营商
     */
    public static int getTelephoneOperator(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String operator = telephonyManager.getSimOperator();
        if (operator != null) {
            if (operator.equals("46000") || operator.equals("46002")) {
                // 中国移动
                return CHINA_MOBILE_CMCC;
            } else if (operator.equals("46001")) {
                // 中国联通
                return CHINA_UNICOM;
            } else if (operator.equals("46003")) {
                // 中国电信
                return CHINA_TELECOM;
            }
        }
        return UNKONW;
    }
}
